#!/bin/bash

# ============================================
# 更新 Nginx 配置路径
# ============================================

DIST_PATH="/mnt/lwy/django-vue3-admin/web/dist"
NGINX_CONF="/etc/nginx/conf.d/frontend.conf"

echo "更新 Nginx 配置..."

# 验证路径是否存在
if [ ! -d "$DIST_PATH" ] || [ ! -f "$DIST_PATH/index.html" ]; then
    echo "错误: dist 路径不存在或没有 index.html"
    exit 1
fi

echo "✓ 找到 dist 目录: $DIST_PATH"

# 更新配置
sudo sed -i "s|root.*;|root $DIST_PATH;|" "$NGINX_CONF"

echo "✓ 配置已更新"

# 显示更新后的配置
echo ""
echo "更新后的 root 路径:"
grep "root" "$NGINX_CONF"

# 验证配置
echo ""
echo "验证配置..."
if sudo nginx -t; then
    echo "✓ 配置测试通过"
else
    echo "✗ 配置测试失败"
    exit 1
fi

# 重新加载
echo ""
echo "重新加载 Nginx..."
sudo systemctl reload nginx

echo ""
echo "✓ 完成！现在访问 http://10.170.129.4 应该可以看到前端页面了"

